package o;

import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.content.res.Resources.Theme;
import android.os.Build.VERSION;
import androidx.annotation.NonNull;
import androidx.annotation.RestrictTo;
import androidx.annotation.RestrictTo.d;
import java.lang.ref.WeakReference;
import java.util.ArrayList;

@RestrictTo({d.LIBRARY_GROUP})
public class ct extends ContextWrapper {
    private static final Object ˋ = new Object();
    private static ArrayList<WeakReference<ct>> ˏ;
    private final Theme ˎ;
    private final Resources ॱ;

    public static Context ˋ(@NonNull Context context) {
        if (!ˊ(context)) {
            return context;
        }
        synchronized (ˋ) {
            Context context2;
            if (ˏ == null) {
                ˏ = new ArrayList();
            } else {
                int size;
                WeakReference weakReference;
                for (size = ˏ.size() - 1; size >= 0; size--) {
                    weakReference = (WeakReference) ˏ.get(size);
                    if (weakReference == null || weakReference.get() == null) {
                        ˏ.remove(size);
                    }
                }
                size = ˏ.size() - 1;
                while (size >= 0) {
                    weakReference = (WeakReference) ˏ.get(size);
                    context2 = weakReference != null ? (ct) weakReference.get() : null;
                    if (context2 == null || context2.getBaseContext() != context) {
                        size--;
                    } else {
                        return context2;
                    }
                }
            }
            context2 = new ct(context);
            ˏ.add(new WeakReference(context2));
            return context2;
        }
    }

    private static boolean ˊ(@NonNull Context context) {
        if ((context instanceof ct) || (context.getResources() instanceof da) || (context.getResources() instanceof dd)) {
            return false;
        }
        if (VERSION.SDK_INT < 21 || dd.ˋ()) {
            return true;
        }
        return false;
    }

    private ct(@NonNull Context context) {
        super(context);
        if (dd.ˋ()) {
            this.ॱ = new dd(this, context.getResources());
            this.ˎ = this.ॱ.newTheme();
            this.ˎ.setTo(context.getTheme());
            return;
        }
        this.ॱ = new da(this, context.getResources());
        this.ˎ = null;
    }

    public Theme getTheme() {
        return this.ˎ == null ? super.getTheme() : this.ˎ;
    }

    public void setTheme(int i) {
        if (this.ˎ == null) {
            super.setTheme(i);
        } else {
            this.ˎ.applyStyle(i, true);
        }
    }

    public Resources getResources() {
        return this.ॱ;
    }

    public AssetManager getAssets() {
        return this.ॱ.getAssets();
    }
}
